package com.msh.frame.web.util;

import com.msh.frame.common.util.StringUtil;
import com.msh.frame.client.define.StringDef;
import org.springframework.util.StreamUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

public class HttpServletRequestUtil {

  public static Long getTenantId() {
    return getTenantId(getCurrentRequest());
  }

  public static String getTraceId() {
    return getTraceId(getCurrentRequest());
  }

  public static Long getAccountId() {
    return getAccountId(getCurrentRequest());
  }

  public static Long getTenantId(HttpServletRequest request) {
    if (null != request) {
      String header = request.getHeader(StringDef.TENANT_ID);
      if (null != header) {
        return Long.valueOf(header);
      }
    }
    return null;
  }

  public static String getTraceId(HttpServletRequest request) {
    if (null != request) {
      return request.getHeader(StringDef.TRACE_ID);
    }
    return null;
  }

  public static Long getAccountId(HttpServletRequest request) {
    if (null != request) {
      String header = request.getHeader(StringDef.ACCOUNT_ID);
      if (null != header) {
        return Long.valueOf(header);
      }
    }
    return null;
  }

  public static String getRequestIp(HttpServletRequest request) {
    String ip = request.getHeader("X-Forwarded-For");

    if (StringUtil.isNotEmpty(ip)) {
      if (ip.indexOf(',') > 0) {
        ip = ip.substring(0, ip.indexOf(","));
      }
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknow".equalsIgnoreCase(ip)) {
      ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getRemoteAddr();
    }
    return ip;
  }

  public static HttpServletRequest getCurrentRequest() {
    ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
        .getRequestAttributes();
    if (null != requestAttributes) {
      return requestAttributes.getRequest();
    }
    return null;
  }


  public static String getRequestPostData(ServletRequest request) {
    try {
      InputStream inputStream = request.getInputStream();
      String body = StreamUtils.copyToString(inputStream, Charset.forName("UTF-8"));
      return body;
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  public static Map<String, String> getHttpServletRequestCookie(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    if (null == cookies || 0 == cookies.length) {
      return new HashMap<>(0);
    }
    Map<String, String> result = new HashMap<String, String>(request.getCookies().length * 3 / 2);
    for (Cookie cookie : cookies) {
      result.put(cookie.getName(), cookie.getValue());
    }
    return result;
  }

  public static String getHttpServletRequestCookieValueByName(String name,
      HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    if (null != cookies) {
      for (Cookie cookie : cookies) {
        if (cookie.getName().equals(name)) {
          return cookie.getValue();
        }
      }
    }
    return null;
  }
}
